#!/usr/bin/env python
# -*- coding: utf-8 -*-

from Shopping.core import accounts

def login_required(func):
    '''验证用户是否登录'''
    def wrapper(*args, **kwargs):
        if args[0].get('is_authenticated'):
            return func(*args, **kwargs)
        else:
            exit('你的账号没有通过身份认证...')
    return wrapper

def acc_auth(account, password):
    '''
    用户认证
    :param account: 账号
    :param password: 密码
    :return:
    '''
    account_data = accounts.load_user_info(account)
    if account_data:
        if account_data['status'] == 0:
            if account_data['password'] == password:
                return account_data
            else:
                print('用户名或密码错误...')
        elif account_data['status'] == 1:
            print('您的用户已被锁定...')
    else:
        print('%s 用户不存在...' % account)

def acc_check(user_id):
    '''
    判断用户是否存在
    :param user_id:
    :return:
    '''
    account_data = accounts.load_user_info(user_id)
    if account_data:
        return account_data

def acc_login(user_data):
    retry_count = 0
    while user_data['is_authenticated'] is not True and retry_count < 3:
        account = input('请输入账号:').strip()
        password = input('请输入密码:').strip()
        auth = acc_auth(account, password)
        if auth:
            user_data['is_authenticated'] = True
            user_data['account_id'] = account
            return auth
        retry_count += 1